package org.asiainfo.flowable.service.impl;

import org.asiainfo.common.core.utils.SpringUtils;
import org.asiainfo.common.core.utils.StringUtils;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.satoken.utils.LoginHelper;
import org.asiainfo.flowable.domain.bo.WfTaskBo;
import org.asiainfo.flowable.domain.vo.WfCopyVo;
import org.asiainfo.flowable.domain.bo.WfCopyBo;
import org.asiainfo.flowable.domain.WfCopy;
import org.asiainfo.flowable.service.WfCopyService;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.springframework.stereotype.Service;
import org.asiainfo.common.core.utils.MapstructUtils;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.asiainfo.flowable.mapper.WfCopyMapper;
import com.mybatisflex.core.paginate.Page;

import java.util.ArrayList;
import java.util.Map;
import java.util.Collection;
import java.util.List;

/**
 * 流程抄送表(WfCopy)表服务实现类
 *
 * @author dotor-ww
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class WfCopyServiceImpl extends ServiceImpl<WfCopyMapper, WfCopy> implements WfCopyService {

    /**
     * 查询
     *
     * @param wfCopyBo 筛选条件
     * @return 查询结果
     */
    @Override
    public List<WfCopyVo> queryList(WfCopyBo wfCopyBo) {
        Map<String, Object> params = wfCopyBo.getParams();
        QueryWrapper query = query();
        return listAs(query, WfCopyVo.class);
    }

    /**
     * 通过copyId查询单条数据
     *
     * @param copyId 主键
     * @return 实例对象
     */
    @Override
    public WfCopyVo queryById(Long copyId) {
        return getOneAs(query().eq(WfCopy::getCopyId, copyId), WfCopyVo.class);
    }

    /**
     * 分页查询
     *
     * @param wfCopyBo  筛选条件
     * @param pageQuery 分页对象
     * @return 查询结果
     */
    @Override
    public TableDataInfo<WfCopyVo> queryPageList(WfCopyBo wfCopyBo, PageQuery pageQuery) {
        Map<String, Object> params = wfCopyBo.getParams();
        QueryWrapper query = query().between(WfCopy::getCreateTime, params.get("beginTime"), params.get("endTime"),
                ObjectUtil.isNotNull(params.get("beginTime")) && ObjectUtil.isNotNull(params.get("endTime")));
        pageQuery.buildOrder(query);
        Page<WfCopyVo> page = pageAs(pageQuery.buildPage(), query, WfCopyVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增数据
     *
     * @param wfCopyBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insertByBo(WfCopyBo wfCopyBo) {
        WfCopy wfCopy = MapstructUtils.convert(wfCopyBo, WfCopy.class);
        return save(wfCopy);
    }

    /**
     * 修改数据
     *
     * @param wfCopyBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean updateByBo(WfCopyBo wfCopyBo) {
        WfCopy wfCopy = MapstructUtils.convert(wfCopyBo, WfCopy.class);
        return updateById(wfCopy);
    }

    /**
     * 通过主键删除数据
     *
     * @param copyIds 主键
     * @return 是否成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> copyIds) {
        return removeByIds(copyIds);
    }

    /**
     * 创建流程抄送
     *
     * @param wfTaskBo 流程任务业务对象
     * @return 操作结果
     */
    @Override
    public Boolean makeCopy(WfTaskBo wfTaskBo) {
        if (StringUtils.isBlank(wfTaskBo.getCopyUserIds())) {
            return true;
        }
        // 1.获取历史流程实例
        HistoricProcessInstance historicProcessInstance = SpringUtils
            .getBean(HistoryService.class)
            .createHistoricProcessInstanceQuery()
            .processInstanceId(wfTaskBo.getProcInsId())
            .singleResult();
        String[] copyUserIds = wfTaskBo.getCopyUserIds().split(",");
        List<WfCopy> wfCopyList = new ArrayList<>(copyUserIds.length);
        // 2.发起人ID
        Long originatorId = LoginHelper.getUserId();
        // 3.发起人昵称
        String originatorName = LoginHelper.getNickName();
        // 4.抄送标题
        String title = String.join("-", historicProcessInstance.getProcessDefinitionName(), wfTaskBo.getTaskName());
        for (String copyUserId : copyUserIds) {
            WfCopy wfCopy = new WfCopy();
            wfCopy.setTitle(title);
            wfCopy.setProcessId(historicProcessInstance.getProcessDefinitionId());
            wfCopy.setProcessName(historicProcessInstance.getProcessDefinitionName());
            wfCopy.setDeploymentId(historicProcessInstance.getDeploymentId());
            wfCopy.setInstanceId(wfCopy.getInstanceId());
            wfCopy.setTaskId(wfTaskBo.getTaskId());
            wfCopy.setUserId(Long.valueOf(copyUserId));
            wfCopy.setOriginatorId(originatorId);
            wfCopy.setOriginatorName(originatorName);
            wfCopyList.add(wfCopy);
        }
        return saveBatch(wfCopyList);
    }
}
